﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Register.IDAL;
using System.Data.SqlClient;
using System.Data;
using Register.DBUtility;
using Register.Model;

namespace Register.DAL
{
    class RoleModelInfoDAL : IRoleModelInfoDAL
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int  Add(RoleModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into System_roleModel(");
            strSql.Append("Role_ID,ModuleInfo_ID,RoleModel_Describe,RoleModel_AddTime,RoleModel_Recoder,RoleModel_LastAmendTime,RoleModel_LastAmendPerson,RoleModel_State)");
            strSql.Append(" values (");
            strSql.Append("@Role_ID,@ModuleInfo_ID,@RoleModel_Describe,@RoleModel_AddTime,@RoleModel_Recoder,@RoleModel_LastAmendTime,@RoleModel_LastAmendPerson,@RoleModel_State)");
            SqlParameter[] parameters = {
					new SqlParameter("@Role_ID", SqlDbType.Char,5),
					new SqlParameter("@ModuleInfo_ID", SqlDbType.Char,5),
					new SqlParameter("@RoleModel_Describe", SqlDbType.NVarChar,100),
					new SqlParameter("@RoleModel_AddTime", SqlDbType.DateTime),
					new SqlParameter("@RoleModel_Recoder", SqlDbType.VarChar,20),
					new SqlParameter("@RoleModel_LastAmendTime", SqlDbType.DateTime),
					new SqlParameter("@RoleModel_LastAmendPerson", SqlDbType.VarChar,20),
					new SqlParameter("@RoleModel_State", SqlDbType.Char,1)};
            parameters[0].Value = model.Role_ID;
            parameters[1].Value = model.ModuleInfo_ID;
            parameters[2].Value = model.RoleModel_Describe;
            parameters[3].Value = model.RoleModel_AddTime;
            parameters[4].Value = model.RoleModel_Recoder;
            parameters[5].Value = model.RoleModel_LastAmendTime;
            parameters[6].Value = model.RoleModel_LastAmendPerson;
            parameters[7].Value = model.RoleModel_State;

           return      DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RoleModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update System_roleModel set ");
            strSql.Append("RoleModel_Describe=@RoleModel_Describe,");
            strSql.Append("RoleModel_AddTime=@RoleModel_AddTime,");
            strSql.Append("RoleModel_Recoder=@RoleModel_Recoder,");
            strSql.Append("RoleModel_LastAmendTime=@RoleModel_LastAmendTime,");
            strSql.Append("RoleModel_LastAmendPerson=@RoleModel_LastAmendPerson,");
            strSql.Append("RoleModel_State=@RoleModel_State");
            strSql.Append(" where Role_ID=@Role_ID and ModuleInfo_ID=@ModuleInfo_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleModel_Describe", SqlDbType.NVarChar,100),
					new SqlParameter("@RoleModel_AddTime", SqlDbType.DateTime),
					new SqlParameter("@RoleModel_Recoder", SqlDbType.VarChar,20),
					new SqlParameter("@RoleModel_LastAmendTime", SqlDbType.DateTime),
					new SqlParameter("@RoleModel_LastAmendPerson", SqlDbType.VarChar,20),
					new SqlParameter("@RoleModel_State", SqlDbType.Char,1),
					new SqlParameter("@Role_ID", SqlDbType.Char,5),
					new SqlParameter("@ModuleInfo_ID", SqlDbType.Char,5)};
            parameters[0].Value = model.RoleModel_Describe;
            parameters[1].Value = model.RoleModel_AddTime;
            parameters[2].Value = model.RoleModel_Recoder;
            parameters[3].Value = model.RoleModel_LastAmendTime;
            parameters[4].Value = model.RoleModel_LastAmendPerson;
            parameters[5].Value = model.RoleModel_State;
            parameters[6].Value = model.Role_ID;
            parameters[7].Value = model.ModuleInfo_ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string Role_ID, string ModuleInfo_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from System_roleModel ");
            strSql.Append(" where Role_ID=@Role_ID and ModuleInfo_ID=@ModuleInfo_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@Role_ID", SqlDbType.Char,5),
					new SqlParameter("@ModuleInfo_ID", SqlDbType.Char,5)};
            parameters[0].Value = Role_ID;
            parameters[1].Value = ModuleInfo_ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<RoleModel> GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Role_ID,ModuleInfo_ID,RoleModel_Describe,RoleModel_AddTime,RoleModel_Recoder,RoleModel_LastAmendTime,RoleModel_LastAmendPerson,RoleModel_State ");
            strSql.Append(" FROM System_roleModel ");
            strSql.Append(" where  Role_ID=@Role_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@Role_ID", SqlDbType .Char,5)
                                            };
            parameters[0].Value = strWhere;
            return DataSetToList(DbHelperSQL.Query(strSql.ToString(),parameters));
        }
        private List<RoleModel> DataSetToList(DataSet ds)
        {
            List<RoleModel> list = new List<RoleModel>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                RoleModel mi = new RoleModel();
                mi.ModuleInfo_ID = (String)row["ModuleInfo_ID"];
                mi.Role_ID = (String)row["Role_ID"];
                mi.RoleModel_AddTime = (DateTime)row["RoleModel_AddTime"];
                if (row["RoleModel_Describe"]==System.DBNull.Value)
                    mi.RoleModel_Describe = (string)row["RoleModel_Describe"];
                else
                    mi.RoleModel_Describe = "";
                mi.RoleModel_LastAmendPerson = (string)row["RoleModel_LastAmendPerson"];
                mi.RoleModel_LastAmendTime = (DateTime)row["RoleModel_LastAmendTime"];
                mi.RoleModel_Recoder = (string)row["RoleModel_Recoder"];
                mi.RoleModel_State = (string)row["RoleModel_State"];
                list.Add(mi);
            }
            return list;
        }
    }
}
